/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.db.structures;

import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Logger;

import com.google.api.adwords.starterkit.utils.Utils;
import com.google.api.adwords.v201101.cm.UserStatus;

/**
 * The Class KeywordStructure represents all Keywords fields managed by 
 * StarterKit.
 * 
 * @author rodrigo.f.vale@gmail.com  (Rodrigo Vale)
 */
public class KeywordStructure extends CriterionStructure {
  private int localAdGroupId;
  private long adGroupId;
  private String destinationUrl = null;
  private long maxCpc;
  private long maxCpm;
  private boolean negative;
  private UserStatus status;
  private boolean isStatsAvailable;

  private float ctr;
  private long clicks;
  private float cost;
  private long impressions;
  private long conversions;
  private long transactions;

  private boolean isStatusModifiable;
  private boolean isMatchModifiable;
  private boolean isCpcModifiable;
  private boolean isUrlModifiable;
  private boolean isBanned;

  private static final Logger log = Logger.getLogger("StarterKit");
  
  public void setLocalAdGroupId(int localAdGroupId) {
    this.localAdGroupId = localAdGroupId;
  }
  
  public int getLocalAdGroupId() {
    return localAdGroupId;
  }
  
  public void setAdGroupId(long adGroupId) {
    this.adGroupId = adGroupId;
  }
  
  public long getAdGroupId() {
    return adGroupId;
  }
  
  public void setDestinationUrl(String destinationUrl) {
    this.destinationUrl = destinationUrl;
  }
  
  public String getDestinationUrl() {
    return destinationUrl;
  }
  
  public void setMaxCpc(long maxCpc) {
    this.maxCpc = maxCpc;
  }
  
  public long getMaxCpc() {
    return maxCpc;
  }
  
  public void setMaxCpm(long maxCpm) {
    this.maxCpm = maxCpm;
  }
  
  public long getMaxCpm() {
    return maxCpm;
  }
  
  public void setNegative(boolean negative) {
    this.negative = negative;
  }
  
  public boolean isNegative() {
    return negative;
  }
  
  public void setStatus(UserStatus status) {
    this.status = status;
  }
  
  public UserStatus getStatus() {
    return status;
  }
  
  public void setHasStats(boolean isStatsAvailable) {
    this.isStatsAvailable = isStatsAvailable;
  }
  
  public boolean isStatsAvailable() {
    return isStatsAvailable;
  }

  public void setCtr(float ctr) {
    this.ctr = ctr;
  }

  public float getCtr() {
    return ctr;
  }

  public void setClicks(long clicks) {
    this.clicks = clicks;
  }

  public long getClicks() {
    return clicks;
  }

  public void setCost(float cost) {
    this.cost = cost;
  }

  public float getCost() {
    return cost;
  }

  public void setImpressions(long impressions) {
    this.impressions = impressions;
  }

  public long getImpressions() {
    return impressions;
  }

  public void setConversions(long conversions) {
    this.conversions = conversions;
  }

  public long getConversions() {
    return conversions;
  }

  public void setTransactions(long transactions) {
    this.transactions = transactions;
  }

  public long getTransactions() {
    return transactions;
  }

  public void setStatusModifiable(boolean isStatusModifiable) {
    this.isStatusModifiable = isStatusModifiable;
  }

  public boolean isStatusModifiable() {
    return isStatusModifiable;
  }

  public void setMatchModifiable(boolean isMatchModifiable) {
    this.isMatchModifiable = isMatchModifiable;
  }

  public boolean isMatchModifiable() {
    return isMatchModifiable;
  }

  public void setCpcModifiable(boolean isCpcModifiable) {
    this.isCpcModifiable = isCpcModifiable;
  }

  public boolean isCpcModifiable() {
    return isCpcModifiable;
  }

  public void setUrlModifiable(boolean isUrlModifiable) {
    this.isUrlModifiable = isUrlModifiable;
  }

  public boolean isUrlModifiable() {
    return isUrlModifiable;
  }

  public void setBanned(boolean isBanned) {
    this.isBanned = isBanned;
  }

  public boolean isBanned() {
    return isBanned;
  }
  
  /**
   * Normalize the different ways to represent criteria status in AdWords API.
   */ 
  public static UserStatus normalizeStatus(String status) {
    if (status.toUpperCase().equals("ENABLED")) {
      return UserStatus.ACTIVE;
    } else if (status.toUpperCase().equals("DISABLE")) {
      return UserStatus.PAUSED;
    } else {
      return UserStatus.fromString(status.toUpperCase());
    } 
  }
  
  public static boolean validateMatchType(String status) {
    String valids[] = {"ACTIVE", "PAUSED", "DELETED"};
    if (!ArrayUtils.contains(valids, status)) {
      log.error("Wrong Keyword status:" + status);
      log.error("Valids status are: " + Utils.toString(valids));
      throw new RuntimeException();
    }
    return true;
  }  
}